/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-05-16
 * Time: 22:16
 */
public class MyLinkedList {
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    public void addFirst(int data){
        ListNode cur=new ListNode(data);
        if (head==null){
            head=cur;
            last=cur;
            return;
        }else {
            cur.next = head;
            head.prev = cur;
            head = cur;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode cur=new ListNode(data);
        if (head==null){
            head=cur;
            last=cur;
            return;
        }else {
            cur.prev=last;
            last.next=cur;
            last=cur;
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode tmp=head;
        int len=0;
        while (tmp!=null){
            len++;
            tmp=tmp.next;
        }
        return len;
    }
    public void display(){
        ListNode tmp=head;
        while (tmp!=null) {
            System.out.print(tmp.val+"  ");
            tmp=tmp.next;
        }
        System.out.println();
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode tmp=head;
        while (tmp!=null){
            if (tmp.val==key){
                return true;
            }
            tmp=tmp.next;
        }
        return false;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode tmp=head;
        if (index<0||index>size()){
            throw new IndexOutofBounds("异常");
        }
        if (index==0){
            addFirst(data);
            return;
        }
        if (index==size()){
            addLast(data);
            return;
        }
        while (index>0){
            index--;
            tmp=tmp.next;
        }
        ListNode cur=new ListNode(data);
        cur.next=tmp;
        cur.prev=tmp.prev;
        tmp.prev.next=cur;
        tmp.prev=cur;
    }
    public void remove(int key){
        ListNode cur=head;
        while (cur!=null){
            if (cur.val==key){
                if (cur==head){
                    head=head.next;
                    if (head!=null){
                        head.prev=null;
                    }else {
                        last=null;
                    }
                }else {
                    if (cur.next==null){
                        cur.prev.next=cur.next;
                        last=last.prev;
                    }else {
                        cur.next.prev=cur.prev;
                        cur.prev.next=cur.next;
                    }
                }
                return;
            }else {
                cur=cur.next;
            }
        }
    }
    public void removeAllKey(int key){
        ListNode cur=head;
        while (cur!=null){
            if (cur.val==key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    }else {
                        last=null;
                    }
                } else {
                    if (cur.next != null) {
                        last = last.prev;
                        last.next=null;
                    } else {
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    }
                }
            }
            cur=cur.next;
        }
    }
}